package cn.com.pc.content.controller;

import cn.com.pc.aaa.Constants;
import cn.com.pc.aaa.model.Identity;
import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.metadata.ContentProperty;
import cn.com.pc.content.domain.metadata.ContentPropertyAlreadyExistsException;
import cn.com.pc.content.domain.metadata.MetadataNotFoundException;
import cn.com.pc.content.dto.ContentPropertyDTO;
import cn.com.pc.content.dto.PageForm;
import cn.com.pc.content.dto.PageResult;
import cn.com.pc.content.dto.Sort;
import cn.com.pc.content.service.MetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 内容Controller
 */
@RestController
@RequestMapping("/metadata")
public class MetadataController {

    @Autowired
    private MetadataService metadataService;


    /**
     * 新增元数据配置
     * @param site 站点
     * @param biz 业务
     * @param contentPropertyDTO 元数据配置
     * @return
     */
    @PostMapping
    public Long createContentProperty(@RequestAttribute(Constants.AAA_IDENTITY) Identity identity, @RequestAttribute("site") Site site, @RequestAttribute("biz") Biz biz, @Validated @RequestBody ContentPropertyDTO contentPropertyDTO) {
        ContentProperty property = metadataService.getContentProperty(contentPropertyDTO.getName(), site, biz, contentPropertyDTO.getContentType());
        boolean exixtsContentProperty = metadataService.exixtsContentProperty(contentPropertyDTO.getName(), site, contentPropertyDTO.getContentType(), contentPropertyDTO.getDataType());
        if (property != null || exixtsContentProperty) {
            throw new ContentPropertyAlreadyExistsException();
        }
        contentPropertyDTO.setSite(site);
        contentPropertyDTO.setBiz(biz);
        contentPropertyDTO.setCreatedAt(LocalDateTime.now());
        contentPropertyDTO.setCreatedBy(identity.getName());
        return metadataService.createContentProperty(contentPropertyDTO);
    }


    /**
     * 查询元数据配置列表
     * @param site
     * @param biz
     * @param contentPropertyDTO
     * @param pageForm
     * @param sort
     * @return
     */
    @GetMapping
    public PageResult<ContentProperty> listContentPropertyByDTO(@RequestAttribute("site") Site site, @RequestAttribute(value = "biz", required = false) Biz biz, ContentPropertyDTO contentPropertyDTO, PageForm pageForm, Sort sort) {
        contentPropertyDTO.setSite(site);
        contentPropertyDTO.setBiz(biz);
        return metadataService.listContentPropertyByDTO(contentPropertyDTO, pageForm, sort);
    }


    /**
     * 删除元数据配置
     * @param propertyId
     */
    @DeleteMapping("/{propertyId:\\d+}")
    public void deleteContentPropertyById(@RequestAttribute("site") Site site, @PathVariable("propertyId") Long propertyId) {
        ContentProperty contentProperty = metadataService.findContentPropertyById(propertyId);
        if (contentProperty == null || contentProperty.isDeleted() || contentProperty.getSite() != site) {
            throw new MetadataNotFoundException();
        }
        contentProperty.setDeleted(true);
        metadataService.updateContentProperty(contentProperty);
    }
}
